package cn.tpshion.config.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

/**
 * 统一异常处理类
 */
@RestControllerAdvice
@Slf4j
public class ExceptionAdvice {

    /**
     * 处理系统异常
     * 指的是500、404等非业务异常
     */
    @ExceptionHandler(Exception.class)
    public R systemException(Exception e){

        if(e instanceof BindException){
            // 请求参数校验异常
            BindException exception = (BindException) e;
            ObjectError objectError = exception.getAllErrors().get(0);
            log.error("param exception: errorMsg={}", exception.getFieldError());
            return R.fail(ErrorCode.PARAM_ERROR, objectError.getDefaultMessage());
        }else if(e instanceof HttpMessageNotReadableException){
            // 请求体参数为空异常
            HttpMessageNotReadableException ex = (HttpMessageNotReadableException) e;
            log.error("HttpMessageNotReadableException: errorMsg={}",ex.getMessage());
            return R.fail(ErrorCode.REQUEST_BODY_NULL);
        }else if(e instanceof HttpMediaTypeNotSupportedException){
            log.error("HttpMediaTypeNotSupportedException: errorMsg={}",e.getMessage());
            return R.fail(ErrorCode.CONTENT_TYPE_ERROR);
        }else if(e instanceof MethodArgumentNotValidException){
            // 请求参数校验异常
            MethodArgumentNotValidException ex = (MethodArgumentNotValidException) e;
            FieldError fieldError = ex.getBindingResult().getFieldErrors().get(0);
            log.error("paramException: errorMsg={}",ex.getMessage());
            return R.fail(ErrorCode.PARAM_ERROR, fieldError.getDefaultMessage());
        } else if(e instanceof MaxUploadSizeExceededException){
            //上传文件超限制异常
            MaxUploadSizeExceededException ex = (MaxUploadSizeExceededException) e;
            log.error("file upload size exception:{}",e.getMessage());
            return R.fail(ErrorCode.UPLOAD_MAX_ERROR);
        } else{
            log.error("system exception: errorMsg={}",e.getMessage(),e);
            return R.fail(ErrorCode.SYSTEM_ERROR);
        }
    }

    /**
     * 处理业务异常
     */
    @ExceptionHandler(BusinessException.class)
    public R coreException(Exception e){
        BusinessException businessException = (BusinessException) e;
        log.error("BusinessException: errorCode={},errorMsg={}",businessException.getCode(),businessException.getMsg());
        return new R(businessException.getCode(), businessException.getMsg(), null);
    }
}
